<?php
class EpalShopAdmin extends ModelAdmin {
    private static $managed_models = array('ECategory', 'EProduct', 'EProduct'); // Can manage multiple models
    private static $url_segment = 'epal-shop'; // Linked as /admin/products/
    private static $menu_title = 'ePal Shop';

    public function getSearchContext()
    {
        $context = parent::getSearchContext();

        if($this->modelClass == 'EProduct') {
            $categories = ECategory::get()->map('ID', 'Title')->toArray();
            $categories = array_merge(array(0 => 'No Category'), $categories);

            $categoryField = new DropdownField('ECategoryID', 'Category', $categories);
            $categoryField->setEmptyString('All');

            $context->addField($categoryField);
        }

        return $context;
    }

    public function getList() {
        $list = parent::getList();
        $params = $this->request->requestVar('ECategoryID'); // use this to access search parameters

        if($this->modelClass == 'EProduct') {
            if($params == '' || $params == null){

            }else if($params == 0){
                $list = $list->leftJoin('EProduct_ECategories', "EProduct.ID = EProduct_ECategories.EProductID")->where('EProduct_ECategories.ECategoryID is null');
            }else{
                $list = $list->leftJoin('EProduct_ECategories', "EProduct.ID = EProduct_ECategories.EProductID")->filter(array('EProduct_ECategories.ECategoryID' => $params));
            }

        }
        return $list;
    }

    public function getEditForm($id = null, $fields = null) {

        $list = $this->getList();

        $exportButton = new GridFieldExportButton('before');
        $exportButton->setExportColumns($this->getExportFields());
        $listField = GridField::create(
            $this->sanitiseClassName($this->modelClass),
            false,
            $list,
            $fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
                ->addComponent($exportButton)
                ->removeComponentsByType('GridFieldFilterHeader')
                ->addComponents(new GridFieldPrintButton('before'))
        );

        if($this->modelClass == 'EProduct') {
//            $fieldConfig->removeComponentsByType('GridFieldAddNewButton');
            $fieldConfig->removeComponentsByType('GridFieldDeleteAction');
//            $fieldConfig->removeComponentsByType('GridFieldEditButton');
        }


        // Validation
        if(singleton($this->modelClass)->hasMethod('getCMSValidator')) {
            $detailValidator = singleton($this->modelClass)->getCMSValidator();
            $listField->getConfig()->getComponentByType('GridFieldDetailForm')->setValidator($detailValidator);
        }

        $form = CMSForm::create(
            $this,
            'EditForm',
            new FieldList($listField),
            new FieldList()
        )->setHTMLID('Form_EditForm');

        $form->setResponseNegotiator($this->getResponseNegotiator());
        $form->addExtraClass('cms-edit-form cms-panel-padded center');
        $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
        $editFormAction = Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'EditForm');
        $form->setFormAction($editFormAction);
        $form->setAttribute('data-pjax-fragment', 'CurrentForm');

        $this->extend('updateEditForm', $form);

        return $form;

    }


}